/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Whitemice.Calendar;
using Gtk;

namespace Whitemice.CalendarControl
{
  [System.ComponentModel.Category("Calendar")]
  [System.ComponentModel.ToolboxItem(true)]
  public partial class DayStripController : Gtk.Bin
  {
    public DayStripController()
    {
      this.Build();
      days.Value = strip.Span;
    } // end ctor
    
    public IList<CalendarEvent> CalendarEvents 
    { 
      get { return strip.CalendarEvents; } 
    } // end CalendarEvents
    
    public void SetPositionToCurrent()
    {
      strip.SetPositionToCurrent();
    } // end SetPositionToCurrent
 
    public DateTime FirstDay
    {
      get { return strip.FirstDay; }
      set 
      { 
        strip.FirstDay = value;
        days.Value = strip.Span;
        SetTitle();
      }
    } // end FirstDay 
    
    public DateTime LastDate
    {
      get { return FirstDay.AddDays(Span); }
    }
    
    public int Span
    { 
      get 
      { 
        return strip.Span; 
      }
      set
      {
        strip.Span = value;
        SetTitle();
      }
    } // end Span
    
    protected void SetTitle()
    {
      if ( strip.Span == 1)
      {
        title.Text = FirstDay.ToString("yyyy-MM-dd");
      } else
        {
          title.Text = string.Format("{0} - {1}", 
                                     strip.FirstDay.ToString("yyyy-MM-dd"), 
                                     strip.LastDay.ToString("yyyy-MM-dd"));
        }
    } // end SetTitle 
    
    protected void MoveStrip(int _days)
    {
      /* TODO protect against running over MinValue and MaxValue */
      strip.FirstDay = strip.FirstDay.AddDays(_days);
      SetTitle();
    } // end MoveStrip 

    protected virtual void OnFastBackClicked (object sender, System.EventArgs e)
    {
      MoveStrip(days.ValueAsInt * -1);
    } // end OnFastBackClicked 

    protected virtual void OnBackClicked (object sender, System.EventArgs e)
    {
      MoveStrip(-1);
    } // end OnBackClicked 

    protected virtual void OnForwardClicked (object sender, System.EventArgs e)
    {
      MoveStrip(1);
    } // end OnForwardClicked 

    protected virtual void OnFastForwardClicked (object sender, System.EventArgs e)
    {
      MoveStrip(days.ValueAsInt);
    } // end OnFastForwardClicked *

    protected virtual void OnDaysValueChanged (object sender, System.EventArgs e)
    {
      Span = days.ValueAsInt;
    } // end OnDaysValueChanged 
  }
}
